package easy;

import util.PrintUtils;

public class Solution_905 {
    public int[] sortArrayByParity(int[] nums) {
        for (int i = 0, j = nums.length - 1; i < j; ) {
            if (nums[i] % 2 == 0) {
                if (nums[j] % 2 == 1) {
                    j--;
                }
                i++;
            } else {
                if (nums[j] % 2 == 0) {
                    nums[i] = nums[i] ^ nums[j];
                    nums[j] = nums[i] ^ nums[j];
                    nums[i] = nums[i] ^ nums[j];
                    i++;
                }
                j--;
            }
        }
        return nums;
    }

    public static void main(String[] args) {
        Solution_905 model = new Solution_905();
        PrintUtils.arrayInt(model.sortArrayByParity(new int[]{3, 1, 2, 4}));
        PrintUtils.arrayInt(model.sortArrayByParity(new int[]{3, 5, 2, 1, 2, 4}));
        PrintUtils.arrayInt(model.sortArrayByParity(new int[]{3, 5}));
        PrintUtils.arrayInt(model.sortArrayByParity(new int[]{2, 4}));
        PrintUtils.arrayInt(model.sortArrayByParity(new int[]{4, 3}));
    }
}
